package isd.com.smarthome.adapter;

import isd.com.smarthome.R;
import isd.com.smarthome.model.Switches;
import isd.com.smarthome.provider.SmartHomeProvider;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

public class SwitchAdapter extends ArrayAdapter<Switches> {
	private Context mContext;
	private List<Switches> mlist;
	private int[] icon ={R.drawable.icon_aircon,R.drawable.icon_light,R.drawable.icon_camera};

	private int status;
	String test;
	
	
	public SwitchAdapter(Context context, int layout, List<Switches> list) {
		super(context, layout, list);
		// TODO Auto-generated constructor stub
		this.mContext = context;
		this.mlist = list;
	}



	@Override
	public View getView(final int position, View view, ViewGroup parent) {
		// TODO Auto-generated method stub
		 Switches switches = mlist.get(position);
		 ViewHolder holder = new ViewHolder();
		if (view == null) {
			LayoutInflater inflater = LayoutInflater.from(mContext);
			view = inflater.inflate(R.layout.switchitem, null);
			
			holder.tbt_switch = (ToggleButton) view.findViewById(R.id.tbt_switch);
			holder.tv_nameitem = (TextView) view.findViewById(R.id.tv_switchname);
			holder.img_group = (ImageView) view.findViewById(R.id.im_group);
			status =switches.getStatusSwitch();
			if (status == 1) {
				holder.tbt_switch.setChecked(true);
			}else{
				holder.tbt_switch.setChecked(false);
			}		
			
			holder.img_group.setBackgroundResource(icon[switches.getIdGroup()]);
			holder.tv_nameitem.setText(switches.getNameSwitch());
			holder.tbt_switch.setBottom(position);
			view.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.v("", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
				}
			});
			
			holder.tbt_switch.setOnClickListener(new OnClickListener() {             
				@Override  
	            public void onClick(View v) {
	            	
	            	Switches switem = mlist.get(position);
	            	if(switem.getStatusSwitch()==1){
	            		switem.setStatusSwitch(0);            		
	            		ContentValues data = SmartHomeProvider.getcontentValuesSwitch(switem);
	            		mContext.getContentResolver().update(SmartHomeProvider.SWITCHES_CONTENT_URI, data, SmartHomeProvider.SwitchTable.ID_SWITCH+"="+switem.getIdSwitch(), null);
	            	}else{
	            		switem.setStatusSwitch(1);
	            		ContentValues data = SmartHomeProvider.getcontentValuesSwitch(switem);
	            		mContext.getContentResolver().update(SmartHomeProvider.SWITCHES_CONTENT_URI, data, SmartHomeProvider.SwitchTable.ID_SWITCH+"="+switem.getIdSwitch(), null);
	            	
	            	}
	            	// có id Switches roi doi status roi truyen data
	            	
//	                Toast.makeText(mContext, "Đã đã click vào icon " + switem.getNameSwitch(), Toast.LENGTH_SHORT).show();
	            }
	        });
			view.setTag(holder);
		}else {
			holder = (ViewHolder) view.getTag();
		}
	

		return view;
	}



}
